{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import datasets\n",
    "import pandas as pd\n",
    "\n",
    "# Load the iris dataset (classification)\n",
    "iris = datasets.load_iris()\n",
    "iris_target_name = 'Species'\n",
    "iris_feature_names = iris.feature_names\n",
    "iris_df = pd.DataFrame(iris.data, columns=iris_feature_names)\n",
    "iris_df[iris_target_name] = iris.target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyspark.sql import SparkSession\n",
    "from pyspark.ml.classification import RandomForestClassifier\n",
    "from pyspark.ml.feature import VectorAssembler\n",
    "from pyspark.ml import Pipeline\n",
    "\n",
    "# Initialize a spark session\n",
    "spark = SparkSession.builder.getOrCreate()\n",
    "df = spark.createDataFrame(iris_df)\n",
    "df.show(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "seed = 123456\n",
    "df_train, df_test = df.randomSplit([0.7, 0.3], seed=seed)\n",
    "assembler = VectorAssembler(inputCols=iris_feature_names,\n",
    "                            outputCol='features')\n",
    "\n",
    "# Train a Random Forest classification model\n",
    "rf = RandomForestClassifier().setLabelCol(iris_target_name)\n",
    "pipe = Pipeline(stages=[assembler, rf])\n",
    "model = pipe.fit(df_train)\n",
    "df_pred = model.transform(df_test)\n",
    "df_pred.show(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from daas_client import DaasClient\n",
    "\n",
    "# Please, change to your URL of Daas server, and credentials\n",
    "url = 'https://192.168.64.3:31753'\n",
    "username = 'admin'\n",
    "password = 'password'\n",
    "project = 'Examples'\n",
    "\n",
    "# Initiate a client of DaaS server, and set the created \"Examples\" project\n",
    "client = DaasClient(url, username, password)\n",
    "if not client.project_exists(project):\n",
    "    client.create_project(project, 'examples', 'This is an example project')\n",
    "client.set_project(project)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pprint import pprint\n",
    "\n",
    "model_name = 'spark-cls'\n",
    "\n",
    "# Publish the built model into DaaS\n",
    "publish_resp = client.publish(model,\n",
    "                              name=model_name,\n",
    "                              mining_function='classification',\n",
    "                              data_test=df_test,\n",
    "                              description='A Spark classification model')\n",
    "pprint(publish_resp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Try to test the published model\n",
    "test_resp = client.test(model_name, publish_resp['model_version'])\n",
    "pprint(test_resp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Call the test REST API above, 'model_name' is required in payload because the test runtime serves multiple models \n",
    "# in a project. \n",
    "import requests\n",
    "\n",
    "bearer_token = 'Bearer {token}'.format(token=test_resp['access_token'])\n",
    "payload = {\n",
    "    'args': {'X': [{'petal length (cm)': 1.5,\n",
    "                    'petal width (cm)': 0.2,\n",
    "                    'sepal length (cm)': 4.6,\n",
    "                    'sepal width (cm)': 3.1}],\n",
    "             'model_name': model_name,\n",
    "             'model_version': publish_resp['model_version']}}\n",
    "\n",
    "response = requests.post(test_resp['endpoint_url'],\n",
    "                         headers={'Authorization': bearer_token},\n",
    "                         json=payload,\n",
    "                         verify=False)\n",
    "pprint(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Deploy the published model into product\n",
    "deploy_resp = client.deploy(model_name, \n",
    "                            deployment_name=model_name + '-svc', \n",
    "                            model_version=publish_resp['model_version'])\n",
    "pprint(deploy_resp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Call the product REST API above, the deployment runtime(s) serve the deployed model dedicatedly.\n",
    "deploy_bearer_token = 'Bearer {token}'.format(token=deploy_resp['access_token'])\n",
    "deploy_payload = {\n",
    "    'args': {'X': [{'petal length (cm)': 1.5,\n",
    "                    'petal width (cm)': 0.2,\n",
    "                    'sepal length (cm)': 4.6,\n",
    "                    'sepal width (cm)': 3.1}]}}\n",
    "\n",
    "response = requests.post(deploy_resp['endpoint_url'],\n",
    "                         headers={'Authorization': deploy_bearer_token},\n",
    "                         json=deploy_payload,\n",
    "                         verify=False)\n",
    "pprint(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
